package com.yuqing.sentimentapp.yqlist.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.linkface.common.adaper.CommonViewHolder;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseFragment;
import com.yuqing.sentimentapp.home.bean.RecommendDataBean;
import com.yuqing.sentimentapp.login.bean.LoginResultBean;
import com.yuqing.sentimentapp.utils.SharePrefrenceHelper;
import com.yuqing.sentimentapp.utils.ToastUtil;
import com.yuqing.sentimentapp.widget.jzvideo.Jzvd;
import com.yuqing.sentimentapp.widget.xrecyclerview.XRecyclerView;
import com.yuqing.sentimentapp.yqlist.activity.YuQingDetailActivity;
import com.yuqing.sentimentapp.yqlist.adapter.YqListAdapter;
import com.yuqing.sentimentapp.yqlist.bean.YqListResultBean;
import com.yuqing.sentimentapp.yqlist.bean.YuQingDataBean;
import com.yuqing.sentimentapp.yqlist.presenter.YqListPresenter;
import com.yuqing.sentimentapp.yqlist.presenter.impl.YqListPresenterImpl;
import com.yuqing.sentimentapp.yqlist.view.YqListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class YqListFragment extends BaseFragment implements YqListView<YqListResultBean> {

    @BindView(R.id.recyclerView)
    XRecyclerView mRecyclerView;
    private YqListAdapter mAdapter;
    private List<YuQingDataBean> dataList = new ArrayList<>();
    private YqListPresenter mPresenter;
    private int page = 1;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_yqlist;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setLoadingMoreEnabled(true);
        mRecyclerView.setPullRefreshEnabled(true);

        mAdapter = new YqListAdapter(getActivity(), dataList, new CommonViewHolder.onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {
                YuQingDetailActivity.start(getActivity(), dataList.get(position - 1).getBdArticleId(), position - 1);
                dataList.get(position - 1).setIsRead("1");
                mAdapter.notifyItemChanged(position);
            }

            @Override
            public void onItemLongClickListener(int position) {

            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.refresh();
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadYqData(page, true);
            }

            @Override
            public void onLoadMore() {
                page++;
                loadYqData(page, false);
            }
        });
        mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                Jzvd jzvd = view.findViewById(R.id.videoplayer);
                if (jzvd != null && Jzvd.CURRENT_JZVD != null &&
                        jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {
                    if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
                        Jzvd.resetAllVideos();
                    }
                }
            }
        });

//        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                    Glide.with(mContext).resumeRequests();
//                }else {
//                    Glide.with(mContext).pauseRequests();
//                }
//            }
//        });

        loadYqData(1, true);
    }

    private void loadYqData(int page, boolean isRefresh) {
        LoginResultBean loginResultBean = SharePrefrenceHelper.getLoginResultBean();
        if (null == loginResultBean) {
            return;
        }
        mPresenter.loadYqListData(SharePrefrenceHelper.getLoginMobile(), page, 10, isRefresh);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new YqListPresenterImpl(this);
    }

    @Override
    public void onSuccess(YqListResultBean data) {

    }

    private void stopLoad() {
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();
    }

    @Override
    public void onFailure(String message) {
        ToastUtil.showShort(message);
        stopLoad();
    }

    @Override
    public void returnYqListResult(YqListResultBean result, boolean isRefresh) {
        stopLoad();
        if (null == result || result.getData().size() == 0) {
            if (!isRefresh) {
                mRecyclerView.setNoMore(true);
            }
            return;
        }
        if (isRefresh) {
            dataList.clear();
        }
        dataList.addAll(result.getData());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPause() {
        super.onPause();
        Jzvd.resetAllVideos();
    }
}
